﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class wfCertificateEdit1 : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        static string certificateImg = "";
        private void GetCertificateInfon()
        {
            string djackId = this.GetParamHtValue("djackId");
            using (msBsc.AppLogic.DataAlc.CBizQualificationCertificate certInfo = new AppLogic.DataAlc.CBizQualificationCertificate())
            {
                DataSet ds = certInfo.GetQualificationDjackId(djackId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string strRet = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.ExecJScript("setCertInfo(" + strRet + ")");
                initArea(ds.Tables[0].Rows[0]["COUNTRY_ID"].ToString(), ds.Tables[0].Rows[0]["CITY_ID"].ToString(), ds.Tables[0].Rows[0]["AREA_ID"].ToString());
                certificateImg = ds.Tables[0].Rows[0]["DOCUMENT_IMAGE"].ToString();
            }
        }

        private void updateCertificateInfo()
        {
            string crtificateId = this.GetParamHtValue("crtificateId");
            string certificateNumber = this.GetParamHtValue("certificateNumber");
            string certificatePicture = this.GetParamHtValue("certificatePicture");
            string name = this.GetParamHtValue("name");
            string gender = this.GetParamHtValue("gender");
            string nationId = this.GetParamHtValue("nationId");
            string nationName = this.GetParamHtValue("nationName");
            string countryId = this.GetParamHtValue("countryId");
            string countryName = this.GetParamHtValue("countryName");
            string cityId = this.GetParamHtValue("cityId");
            string cityName = this.GetParamHtValue("cityName");
            string destinationId = this.GetParamHtValue("destinationId");
            string destinationName = this.GetParamHtValue("destinationName");
            string birthDate = this.GetParamHtValue("birthDate");
            string issueDate = this.GetParamHtValue("issueDate");
            string quasiType = this.GetParamHtValue("quasiType");
            string effStartDate = this.GetParamHtValue("effStartDate");
            string endDate = this.GetParamHtValue("endDate");
            string certificateType = this.GetParamHtValue("certificateType");
            string publisherId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(publisherId))
            {
                this.ExecJScript("layer.alert('请登录！', 3);location.reload();");
            }
            if (string.IsNullOrEmpty(crtificateId) || string.IsNullOrEmpty(certificateNumber) || string.IsNullOrEmpty(certificatePicture) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(gender) || string.IsNullOrEmpty(nationId) || string.IsNullOrEmpty(nationName) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(birthDate) || string.IsNullOrEmpty(issueDate) || string.IsNullOrEmpty(quasiType) || string.IsNullOrEmpty(effStartDate) || string.IsNullOrEmpty(endDate) || string.IsNullOrEmpty(certificateType))
            {
                this.ExecJScript("layer.alert('请完善资格证信息！', 3);");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizQualificationCertificate certInfo = new AppLogic.DataAlc.CBizQualificationCertificate())
            {
                if (certificatePicture != certificateImg)
                {
                    using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
                    {
                        //保存到系统永久目录，之前上传时存到临时目录的，也可另写代码拷贝文件到其他目录
                        certificatePicture = oCBizSysFile.SaveImgToServer(""
                            , msBsc.SvRef.UserInfo.Operator
                            , "1"
                            , "VEH"
                            , ""
                            , certificatePicture//文件来源虚拟路径
                            , ""//文件来源物理路径，虚拟和物理只有一个就行
                            , true);
                    }
                }
                string strRet = certInfo.UpdateCertificate(crtificateId, certificateNumber, certificatePicture, name, gender, nationId, nationName, countryId, countryName, cityId, cityName, destinationId, destinationName, birthDate, issueDate, quasiType, effStartDate, endDate, certificateType);
                if (strRet != "OK")
                {
                    this.Alert(strRet);
                    return;
                }
                this.ExecJScript(@"success();");
            }
        }

        private void getArea2()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string cityId = "";
            string destionId = "";
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selCity"",'" + cityId + "');");

                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "请选择";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selDestination"",'" + destionId + "');");
            }
        }

        private void getArea3()
        {
            string parentsId = this.GetParamHtValue("parentsId");
            string destionId = "";
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "请选择";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selDestination"",'" + destionId + "');");
            }
        }

        private void initArea(string parentsId, string cityId, string destionId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selCity"",'" + cityId + "');");

                ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 0)
                {
                    return;
                }
                DataRow dr = ds.Tables[0].NewRow();
                dr["COUNTRY_CITY_ID"] = "";
                dr["COUNTRY_CITY_NME"] = "请选择";
                ds.Tables[0].Rows.InsertAt(dr, 0);
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"setCity(" + strJson + @",""selDestination"",'" + destionId + "');");
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                GetCertificateInfon();
            }
            else if (this.strAction == "upCountry")
            {
                getArea2();
            }
            else if (this.strAction == "upCity")
            {
                getArea3();
            }
            else if (this.strAction=="editCertificate")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                string cityId = this.GetParamHtValue("cityId");
                string destionId = this.GetParamHtValue("destionId");
                initArea(parentsId, cityId, destionId);
            }
            else if (this.strAction=="saveCerificate")
            {
                updateCertificateInfo();
            }
        }
    }
}